<template>
    <!-- 对话框表单 -->
    <el-dialog class="ba-operate-dialog" :close-on-click-modal="false" :model-value="baTable.form.operate ? true : false" @close="baTable.toggleForm">
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                >
                    <el-form-item :label="t('商家号')">
                        <el-cascader v-model="baTable.form.items!.seller" :options="props.formData.seller" :clearable="true" placeholder="请选择商家" />
                    </el-form-item>
                    <FormItem :label="t('门牌号')" type="string" v-model="baTable.form.items!.house_number" />
                    <FormItem :label="t('商家名')" type="string" v-model="baTable.form.items!.seller_name" />
                    <FormItem :label="t('商品图片')" type="image" v-model="baTable.form.items!.goods_image" />
                    <FormItem :label="t('补充图片')" type="images" v-model="baTable.form.items!.supplement_images" />
                    <FormItem :label="t('颜色')" type="custom" v-model="baTable.form.items!.color" :input-attr="{ placement: 'top', title: '请选择颜色', data: props.formData.color }" />
                    <FormItem :label="t('尺码')" type="custom" v-model="baTable.form.items!.size" :input-attr="{ placement: 'top', title: '请选择尺码', data: props.formData.size }" />
                    <el-form-item :label="t('分类')">
                        <el-cascader v-model="baTable.form.items!.category" :options="props.formData.category" :clearable="true" placeholder="请选择分类" />
                    </el-form-item>
                    <FormItem :label="t('数量')" type="number" @input="count" v-model="baTable.form.items!.number" />
                    <FormItem :label="t('采购价')" type="number" @input="count" v-model="baTable.form.items!.purchase_price" />
                    <FormItem :label="t('总价')" type="number" v-model="baTable.form.items!.total_price" :readonly="true" :input-attr="{ disabled: true, placeholder: '输入数量和采购价后自动计算' }" />
                    <FormItem
                        :label="t('订单选项')"
                        type="radio"
                        v-model="baTable.form.items!.option"
                        prop="type"
                        :data="{ childrenAttr: { border: true }, content: { '0': t('只要现货'), '3': t('接受预定3天'), '7': t('接受预定7天'), '14': t('接受预定14天') } }"
                    />
                    
                    <FormItem
                        :label="t('类型')"
                        type="radio"
                        v-model="baTable.form.items!.type"
                        prop="type"
                        :data="{ childrenAttr: { border: true }, content: { '0': t('待付款'), '1': t('待取货'), '2': t('补货') } }"
                    />
                    <FormItem :label="t('备注')" type="textarea" v-model="baTable.form.items!.note" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { ElForm, FormItemRule } from 'element-plus'


interface Props {
    formData: {
        seller?: anyObj
        color?: anyObj
        size?: anyObj
        category?: anyObj
    }
}

const props = withDefaults(defineProps<Props>(), {
    formData: () => {
        return {}
    },
})

const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass



const { t } = useI18n()

const count = () => {
    let purchase_price = baTable.form.items!.purchase_price == undefined ? 0 : baTable.form.items!.purchase_price;
    purchase_price = purchase_price < 0 ? 0 : purchase_price;
    baTable.form.items!.purchase_price = purchase_price;
    let number = baTable.form.items!.number == undefined ? 0 : baTable.form.items!.number;
    number = number < 0 ? 0 : number;
    baTable.form.items!.number = number;
    baTable.form.items!.total_price = purchase_price * number;
}


</script>

<style scoped lang="scss">
.ba-el-radio {
    margin-bottom: 10px;
}
</style>
